package com.zbkj.admin.controller;

import com.zbkj.common.request.WechatShippingConfigRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.response.WechatShippingResponse;
import com.zbkj.service.service.WechatShippingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 微信发货信息管理配置控制器
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/wechat/shipping")
@Api(tags = "微信发货信息管理")
public class WechatShippingConfigController {

    @Autowired
    private WechatShippingService wechatShippingService;

    /**
     * 获取微信发货配置
     */
    @ApiOperation(value = "获取微信发货配置")
    @RequestMapping(value = "/config", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('admin:wechat:shipping:config:index')")
    public CommonResult<WechatShippingConfigRequest> getConfig() {
        return CommonResult.success(wechatShippingService.getShippingConfig());
    }

    /**
     * 更新微信发货配置
     */
    @ApiOperation(value = "更新微信发货配置")
    @RequestMapping(value = "/config", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('admin:wechat:shipping:config:update')")
    public CommonResult<String> updateConfig(@RequestBody @Validated WechatShippingConfigRequest request) {
        Boolean result = wechatShippingService.updateShippingConfig(request);
        if (result) {
            return CommonResult.success("配置更新成功");
        }
        return CommonResult.failed("配置更新失败");
    }

    /**
     * 检查微信发货功能是否启用
     */
    @ApiOperation(value = "检查微信发货功能是否启用")
    @RequestMapping(value = "/enabled", method = RequestMethod.GET)
    public CommonResult<Boolean> isEnabled() {
        return CommonResult.success(wechatShippingService.isShippingEnabled());
    }

    /**
     * 查询订单发货状态
     */
    @ApiOperation(value = "查询订单发货状态")
    @RequestMapping(value = "/status", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('admin:order:shipping:status')")
    public CommonResult<WechatShippingResponse> getShippingStatus(@RequestParam String orderKey) {
        WechatShippingResponse response = wechatShippingService.getShippingStatus(orderKey);
        return CommonResult.success(response);
    }

    /**
     * 特殊发货报备
     */
    @ApiOperation(value = "特殊发货报备")
    @RequestMapping(value = "/special", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('admin:order:shipping:special')")
    public CommonResult<WechatShippingResponse> specialOrderReport(@RequestParam String orderKey) {
        WechatShippingResponse response = wechatShippingService.specialOrderReport(orderKey);
        return CommonResult.success(response);
    }
} 